/*	game.c 
 *  Arizona State University
 *  CSE 325: Embedded Systems Programming
 *  Lab 7: Morse Code
 *  Georgii Tkachuk, Brentton Garber
 *   Emails: 	gtkachuk@asu.edu btgarber@asu.edu
 * 	Description: the game module specifies the rules of the game, when the Z button is pressed on the nunchuk, the sequence of presses is tracked
 *               then translated into morse code.
 */

#include "common.h"


int timeTrack = 0;
int toneQueue[5];
int toneQueueCount = 0;
int btnState = RELEASED;

// tracks the amount of time that has passed during button presses
void game_trackTime()
{
	timeTrack++;
}

// determines the delay type from the amount of time that has passed
int game_getDelayType()
{
	// if time is above 1 second, we consider this a long pulse
	if(timeTrack > 1000) return LONG;
	else return SHORT;
}

// translates sequences into characters
int game_getSequenceToChar()
{
	int i;
	int letter[][5] =
	{
		{ SHORT, LONG,  NONE,  NONE,  NONE  },
		{ LONG,  SHORT, SHORT, SHORT, NONE  },
		{ LONG,  SHORT, LONG,  SHORT, NONE  },
		{ LONG,  SHORT, SHORT, NONE,  NONE  },
		{ SHORT, NONE,  NONE,  NONE,  NONE  },
		{ SHORT, SHORT, LONG,  SHORT, NONE  },
		{ LONG,  LONG,  SHORT, NONE,  NONE  },
		{ SHORT, SHORT, SHORT, SHORT, NONE  },
		{ SHORT, SHORT, NONE,  NONE,  NONE  },
		{ SHORT, LONG,  LONG,  LONG,  NONE  },
		{ LONG,  SHORT, LONG,  NONE,  NONE  },
		{ SHORT, LONG,  SHORT, SHORT, NONE  },
		{ LONG,  LONG,  NONE,  NONE,  NONE  },
		{ LONG,  SHORT, NONE,  NONE,  NONE  },
		{ LONG,  LONG,  LONG,  NONE,  NONE  },
		{ SHORT, LONG,  LONG,  SHORT, NONE  },
		{ LONG,  LONG,  SHORT, LONG,  NONE  },
		{ SHORT, LONG,  SHORT, NONE,  NONE  },
		{ SHORT, SHORT, SHORT, NONE,  NONE  },
		{ LONG,  NONE,  NONE,  NONE,  NONE  },
		{ SHORT, SHORT, LONG,  NONE,  NONE  },
		{ SHORT, SHORT, SHORT, LONG,  NONE  },
		{ SHORT, LONG,  LONG,  NONE,  NONE  },
		{ LONG,  SHORT, SHORT, LONG,  NONE  },
		{ LONG,  SHORT, LONG,  LONG,  NONE  },
		{ LONG,  LONG,  SHORT, SHORT, NONE  },
		{ SHORT, LONG,  LONG,  LONG,  LONG  },
		{ SHORT, SHORT, LONG,  LONG,  LONG  },
		{ SHORT, SHORT, SHORT, LONG,  LONG  },
		{ SHORT, SHORT, SHORT, SHORT, LONG  },
		{ SHORT, SHORT, SHORT, SHORT, SHORT },
		{ LONG,	 SHORT, SHORT, SHORT, SHORT },
		{ LONG,  LONG,  SHORT, SHORT, SHORT },
		{ LONG,  LONG,  LONG,  SHORT, SHORT },
		{ LONG,  LONG,  LONG,  LONG,  SHORT },
		{ LONG,  LONG,  LONG,  LONG,  LONG  },
		{ NONE,  NONE,  NONE,  NONE,  NONE  }
	};
	
	// search for our sequence
	for(i=0; i<=36; i++)
	{
		if(toneQueue[0] != letter[i][0]) continue;
		if(toneQueue[1] != letter[i][1]) continue;
		if(toneQueue[2] != letter[i][2]) continue;
		if(toneQueue[3] != letter[i][3]) continue;
		if(toneQueue[4] != letter[i][4]) continue;
		return i;
	}
	
	// return 37 if sequence was not found
	return 37;
}

// called when nunchuk is ckecked for button presses
void game_ButtonUpdate(int btnNextState)
{
	int seqType, i;
	seqType = game_getDelayType();
	// if button state has not changed, play a note, then exit
	if(btnState == btnNextState)
	{
		// if the button is being pushed, play a sound
		if(btnState == PRESSED) play_note(-1, 0, 0);
		else play_note(0, 0, 0);
		
		// if we have hit a long delay, light up an led
		if(seqType == LONG) uc_led_all_on();
		else uc_led_all_off();
		
		return;
	}
	
	// Button state has changed
	

	// End of long or short press
	if(btnState == PRESSED)
	{
		// if the tone queue is not full, add a sequence
		if(toneQueueCount < 5)
		{
			toneQueue[toneQueueCount] = seqType;
			toneQueueCount++;
		}
	}
	
	// End of long delay
	else
	{
		// Display letter made by previous sequence
		if(seqType == LONG)
		{
			// get sequence to char, then send it to the led matrix
			ledmatrix_writeChar( game_getSequenceToChar() );
			
			// clear tone sequence and reset count
			for(i=0; i<5; i++) toneQueue[i] = NONE;
			toneQueueCount = 0;
		}
	
		// End of short delay, do nothing for this.
		if(seqType == SHORT) { }
	}
	
	timeTrack = 0;
	btnState = btnNextState;
}